package com.ss.android.article.base.feature.message;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.update.a.e;
import com.ss.android.article.base.feature.update.b.a;
import com.ss.android.article.base.utils.a.SharedPreferenceUtils;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.UrlBuilder;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class n extends a {
    final class n$a extends AsyncTask {
        private final n a;

        n$a(n arg1) {
            this.a = arg1;

        }

        private c a() {
            c v1_1 = null;
            int v0_2;
            long v4 = 0;
            String v1 = null;
            String v0 = this.a.c();
            SharedPreferenceUtils.inst();
            long v10 = SharedPreferenceUtils.getLong(v0, com.ss.android.article.base.feature.message.n.a(v0, "last_time"));
            SharedPreferenceUtils.inst();
            long v6 = SharedPreferenceUtils.getLong(v0, com.ss.android.article.base.feature.message.n.a(v0, "user_id"));
            SharedPreferenceUtils.inst();
            v0 = SharedPreferenceUtils.getString(v0, com.ss.android.article.base.feature.message.n.a(v0, "update_list"), v1);
            ArrayList v9 = new ArrayList();
            if (!android.support.design.a.isTextEmpty(v0)) {
                try {
                    if ((Logger.debug()) && !android.support.design.a.isTextEmpty(v0)) {
                        Logger.d("UpdateMessageListManager LoadListTask", v0);
                    }

                    JSONArray v3 = new JSONArray(v0);
                    int v8 = v3.length();
                    v0_2 = 0;
                    while (true) {
                        if (v0_2 >= v8) {
                            break;
                        }

                        m v12 = com.ss.android.article.base.feature.message.m.b(v3.getJSONObject(v0_2));
                        if (v12 != null) {
                            ((List) v9).add(new e(((com.ss.android.article.base.feature.update.a.a) v12)));
                        }
                        ++v0_2;
                        continue;
                    }
                } catch (Exception v0_1) {
                }


            }

            if (!((List) v9).isEmpty() && v6 > v4) {
                v1_1 = new c(0, false, v4, v6, 2);
                v1_1.f = v9;
                v1_1.g = v10;
            }

            return v1_1;
        }

        protected final Object doInBackground(Object[] arg2) {
            return this.a();
        }

        protected final void onPostExecute(Object arg7) {
            this.a.h = false;
            if (!this.a.o && arg7 != null && ((c) arg7).f != null && !((c) arg7).f.isEmpty() && (this.a.q.isEmpty()) && (this.a.d <= 0 || this.a.d == ((c) arg7).d)) {
                this.a.n.clear();
                this.a.n.addAll(((c) arg7).f);
                if (this.a.g) {
                } else if (!this.a.c.isLogin) {

                } else {
                    this.a.k = ((com.ss.android.article.base.feature.update.a.e) ((c) arg7).f.get(0)).c;
                    this.a.l = ((com.ss.android.article.base.feature.update.a.e) ((c) arg7).f.get(((c) arg7).f.size() - 1)).c;
                    this.a.j = true;
                    this.a.q.addAll(this.a.n);
                    this.a.a(true, 0, -1, null);
                }
            }

            label_13:
            if (this.a.q.isEmpty()) {
                this.a.d();
            }
        }
    }

    final class b extends AsyncTask {
        private final long a;
        private final n d;
        private long b;
        private List<com.ss.android.article.base.feature.update.a.f> c;

        public b(n arg1, long arg2, List arg4, long arg5) {
            this.d = arg1;

            this.a = arg5;
            this.b = arg2;
            this.c = arg4;
        }

        private Void a() {
            Void v6 = null;
            if (!this.c.isEmpty()) {
                try {
                    JSONArray v1 = new JSONArray();
                    Iterator<com.ss.android.article.base.feature.update.a.f> v2 = this.c.iterator();
                    while (v2.hasNext()) {
                        v1.put(new JSONObject(v2.next().a));
                    }

                    String v0_1 = v1.toString();
                    if ((Logger.debug()) && !android.support.design.a.isTextEmpty(v0_1)) {
                        Logger.d("UpdateMessageListManager SaveListTask", v0_1);
                    }

                    String v1_1 = this.d.c();
                    SharedPreferenceUtils.inst();
                    SharedPreferences.Editor v2_1 = SharedPreferenceUtils.getEditor(v1_1);
                    v2_1.putLong(SharedPreferenceUtils.getMigrateKey(v1_1, com.ss.android.article.base.feature.message.n.a(v1_1, "last_time")), this.b);
                    v2_1.putString(SharedPreferenceUtils.getMigrateKey(v1_1, com.ss.android.article.base.feature.message.n.a(v1_1, "update_list")), v0_1);
                    v2_1.putLong(SharedPreferenceUtils.getMigrateKey(v1_1, com.ss.android.article.base.feature.message.n.a(v1_1, "user_id")), this.a);
                    SharedPreferenceCompat.commit(v2_1);
                } catch (Exception v0) {
                    Logger.w("UpdateMessageListManager", "save update list exception: " + v0);
                }
            }

            return v6;
        }

        protected final Object doInBackground(Object[] arg2) {
            return this.a();
        }
    }

    long a;
    boolean b;
    private String s;

    public n(Context arg3) {
        super(arg3, 5);
        this.b = false;
        this.a = 0;
    }

    public n(Context arg3, String arg4) {
        super(arg3, 4);
        this.b = false;
        this.a = 0;
        this.s = arg4;
    }

    static String a(String arg2, String arg3) {
        return arg2 + "@" + arg3;
    }

    public final com.ss.android.article.base.feature.update.a.a a(JSONObject arg2) {
        return com.ss.android.article.base.feature.message.m.b(arg2);
    }

    public final e a(com.ss.android.article.base.feature.update.a.a arg2) {
        return new e(arg2);
    }

    public final String a(c arg10) {
        String v0;
        String v1 = null;
        long v6 = 0;
        UrlBuilder v2 = new UrlBuilder();
        if (this.f == 4) {
            v2.mUrl = ArticleConstants.dongtaiMsgListUrlV2;
        } else if (this.f == 5) {
            v2.mUrl = ArticleConstants.dongtaiNotificationListUrl;
        }

        if (this.a <= v6 && !this.c.isLogin) {
            v0 = v1;
            return v0;
        }

        v2.addParam("count", 20);
        if (this.b) {
            v2.addParam("from_home", 1);
        }

        if (this.a > v6) {
            v2.addParam("user_id", this.a);
        }

        if (arg10.c > v6) {
            v0 = arg10.b ? "min_cursor" : "max_cursor";
            v2.addParam(v0, arg10.c);
        }

        if (!android.support.design.a.isTextEmpty(this.s)) {
            v2.addParam("action_type", this.s);
        }

        v0 = v2.toString();
        int v2_1 = 204800;
        try {
            v0 = NetworkUtils.doGet(v2_1, v0);

            if (!Logger.debug()) {
                return v0;
            }

            if (android.support.design.a.isTextEmpty(v0)) {
                return v0;
            }

            Logger.d("UpdateMessageListManager", v0);
            return v0;
        } catch (Exception v1_1) {
            v1_1.printStackTrace();
            return v0;
        }

    }

    public final void a(long arg8, long arg10, List arg12) {
        new b(this, arg10, arg12, arg8).execute(new Object[0]);
    }

    public final void a(List arg3) {
    }

    public final boolean a() {
        return true;
    }

    public final boolean a(int arg2) {
        boolean v0 = arg2 == 4 || arg2 == 5 ? true : false;
        return v0;
    }

    public final void b() {
        this.m = true;
        this.h = true;
        new n$a(this).execute(new Object[0]);
    }

    protected final String c() {
        String v0;
        if (this.f != 4) {
            v0 = "sp_update_notification";
        } else if ("comment".equals(this.s)) {
            v0 = "sp_update_reply_msg";
        } else if ("digg".equals(this.s)) {
            v0 = "sp_update_digg_msg";
        } else {
            v0 = "sp_update_msg";
        }

        return v0;
    }
}

